﻿using System;
using System.Reflection;
using System.Xml;

namespace XNA.BLEND.parser {
  public enum ActionProp {
      INITIAL,
      [ActionProp( 3, ".location" )] LOC,
      [ActionProp( 3, ".scale" )] SCALE,
      [ActionProp( 3, ".rotation_euler" )] ROT_EUL,
      [ActionProp( 4, ".rotation_quaternion" )] ROT_QUAT,
      [ActionProp( 4, ".rotation_axis_angle" )] ROT_AXIS,
  }

  internal static class ActionPropExtensions {
    internal static ActionProp from( BlendParser parser, dynamic elmFCurve ) {
      ActionProp result = ActionProp.INITIAL;

      string rnapath = parser.Document.FileBlocks[elmFCurve.rna_path].Link.Expression;

      foreach ( ActionProp p in (Enum.GetValues( typeof( ActionProp ) ) ) ) {
        ActionPropAttribute attr = p.GetAttr();
        if ( attr != null && rnapath.EndsWith( attr.Suffix ) ) {
          result = p;
          break;
        }
      }

      return result;
    }

    internal static ActionPropAttribute GetAttr( this ActionProp THIS ) {
      MemberInfo mi = typeof(ActionProp).GetField( Enum.GetName( typeof(ActionProp), THIS ) );

      ActionPropAttribute result = null;
      if ( mi != null ) {
        result = (ActionPropAttribute)Attribute.GetCustomAttribute( mi, typeof( ActionPropAttribute ) );
      }

      return result;
    }
  }

  public class ActionPropAttribute : Attribute {
    public ActionPropAttribute( int size, string suffix ) {
      this.Size = size;
      this.Suffix = suffix;
    }
    public int Size { get; private set; }
    public string Suffix { get; private set; }
  }
}
